<template>
	<view class="goods-list">
		<my-goods :goods="item" v-for="(item,i) in googList" :key="i" @click.native='gotoDetail(item)'></my-goods>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				// 请求参数对象
				queryObj: {
					// 查询关键词
					query: '',
					// 商品分类Id
					cid: '',
					// 页码值
					pagenum: 1,
					// 每页显示多少条数据
					pagesize: 10
				},
				googList: [],
				total: 0,
				isloading: false

			}
		},
		onLoad(options) {
			this.queryObj.query = options.query || ''
			this.queryObj.cid = options.cid || ''
			this.getGoodsList()
		},
		// 页面事件处理函数  处理加载
		onReachBottom() {
			// 判断数据是否加载完毕
			if (this.googList.length === this.total) return uni.$showMsg('数据加载完毕')
			// 判断是否正在请求其它数据，如果是，则不发起额外的请求
			if (this.isloading) return
			this.queryObj.pagenum += 1
			this.getGoodsList()
		},
		// 下拉刷新
		onPullDownRefresh() {
			this.queryObj.pagenum = 1
			this.total = 0
			this.isloading = false
			this.googList = []
			this.getGoodsList(() => uni.stopPullDownRefresh())
		},
		methods: {
			async getGoodsList(cb) {
				this.isloading = true
				const {
					data: res
				} = await uni.$http.get('/goods/search', this.queryObj)
				if (cb) {
					cb()
				}
				this.isloading = false
				if (res.meta.status !== 200) return uni.$showMsg()
				this.googList = [...this.googList, ...res.message.goods]
				this.total = res.message.total
			},
			gotoDetail(goods) {
				uni.navigateTo({
					url: '/subpkg/goods_detail/goods_detail?goods_id=' + goods.goods_id
				})
			}
		}
	}
</script>

<style lang="scss">


</style>
